using System;
using System.Drawing;
using System.Windows.Forms;
using DevExpress.Utils;

namespace NAGFwk.UI
{
    public class ObjectToolTipController : IDisposable
    {
        readonly ToolTipController _controller;
        readonly Control _parent;
        public object EditObject { get; private set; }

        public ObjectToolTipController(Control parent)
        {
            _parent = parent;
            _parent.Disposed += delegate { Dispose(); };
            _controller = new ToolTipController();
            _controller.ToolTipType = ToolTipType.SuperTip;
            _controller.AllowHtmlText = true;
            _controller.ReshowDelay = _controller.InitialDelay;
            _controller.AutoPopDelay = 10000;
            parent.MouseDown += delegate { HideHint(false); };
            parent.MouseLeave += delegate { HideHint(true); };
        }
        public void ShowHint(object editObject, Point location)
        {
            if (Equals(editObject, EditObject))
                return;
            EditObject = editObject;
            ToolTipControlInfo info = new ToolTipControlInfo();
            ToolTipItem item = new ToolTipItem();
            InitToolTipItem(item);
            item.ImageToTextDistance = 10;
            info.Object = DateTime.Now.Ticks;
            info.SuperTip = new SuperToolTip();
            info.SuperTip.Items.Add(item);
            info.ToolTipPosition = _parent.PointToScreen(location);
            _controller.ShowHint(info);
        }
        protected virtual void InitToolTipItem(ToolTipItem item)
        {
        }
        public void HideHint(bool clearCurrentObject)
        {
            if (clearCurrentObject)
                EditObject = null;
            _controller.HideHint();
        }
        #region IDisposable Members
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                _controller.Dispose();
            }
        }
        ~ObjectToolTipController()
        {
            Dispose(false);
        }
        #endregion
    }
}
